/**
 * 
 */

function validateForm() {
	var div = document.getElementById("error");
	while (div.hasChildNodes()) {
		div.removeChild(div.childNodes[0]);
	}
	var foodName = document.forms["drink"]["drinkName"].value;
	var oldPrice = document.forms["drink"]["oldPrice"].value;
	var newPrice = document.forms["drink"]["newPrice"].value;
	var textError = null;
	var check = true;
	if (foodName == null || foodName == "") {
		textError = "Drink name must be filled out";
		addElements(div, textError);
		check = false;
	}
	if (oldPrice == null || oldPrice == "") {
		textError = "Old price name must be filled out";
		addElements(div, textError);
		check = false;
	} else if (oldPrice < 0) {
		textError = "Old price name must be greater than or equal 0";
		addElements(div, textError);
		check = false;
	}
	if (newPrice < 0) {
		textError = "New price name must be greater than or equal 0";
		addElements(div, textError);
		check = false;
	}
	if (checkExistedDrink() == true) {
		textError = "Food name has existed!";
		addElements(div, textError);
		check = false;
	}

	return check;
}

function addElements(div, textError) {
	var dv = document.createElement("DIV");
	dv.setAttribute("class", "alert alert-danger");
	var sp1 = document.createElement("SPAN");
	sp1.setAttribute("class", "glyphicon glyphicon-exclamation-sign");
	dv.appendChild(sp1);
	var text = document.createTextNode(textError);
	dv.appendChild(text);
	div.appendChild(dv);
}

function checkExistedDrink() {
	var flag = false;
	$.ajax({
		"type" : "POST",
		"async" : false,
		"url" : "/core-web-admin/drink/checkexisteddrink",
		"data" : {
			"menuDrinkId" : $('#menuDrinkId').val(),
			"drinkName" : $('#drinkName').val()
		},
		"success" : function(data) {
			if (data == "true") {
				flag = true;
			}
			if (data == "false") {
				flag = false;
			}
		},
		"error" : function(data) {
			alert("ERROR WHEN CHECK EXISTED DRINK NAME!");
			flag = false;
		}
	});
	return flag;
}

function deleteDrink() {
	var flag = false;
	$.ajax({
		"type" : "POST",
		"async" : false,
		"url" : "/core-web-admin/drink/delete",
		"data" : {
			"menuDrinkId" : $('#menuFoodId').val(),
			"drinkId" : $('#drinkId').val()
		},
		"success" : function(data) {
			if (data == "true") {
				flag = true;
			}
			if (data == "false") {
				flag = false;
			}
		},
		"complete" : function(data) {
			location.href = "/core-web-admin/drink/list";
		},
		"error" : function(data) {
			alert("ERROR WHEN CHECK DELETE DRINK NAME!");
			flag = false;
		}
	});
	return flag;
}

function validateUpdateForm() {
	var div = document.getElementById("error");
	while (div.hasChildNodes()) {
		div.removeChild(div.childNodes[0]);
	}
	var oldPrice = document.forms["updatedrink"]["oldPrice"].value;
	var newPrice = document.forms["updatedrink"]["newPrice"].value;
	var textError = null;
	var check = true;
	if (oldPrice == null || oldPrice == "") {
		textError = "Old price name must be filled out";
		addElements(div, textError);
		check = false;
	} else if (oldPrice < 0) {
		textError = "Old price name must be greater than or equal 0";
		addElements(div, textError);
		check = false;
	}
	if (newPrice < 0) {
		textError = "New price name must be greater than or equal 0";
		addElements(div, textError);
		check = false;
	}
	return check;
}

function showImage() {
	var div = document.getElementById('imgs');
	while (div.hasChildNodes()) {
		div.removeChild(div.childNodes[0]);
	}
	var reader = new FileReader();
	reader.onload = function(evt) {
		var img = '<img id="image" src="' + evt.target.result + '" />';
		$('#imgs').append(img);
	};
	reader.readAsDataURL(document.getElementById("drinkImg").files[0]);
}

function addInputImage() {
	var src = document.getElementById('image').getAttribute('src');
	$('#imgs').append(
			'<input type="hidden" id="drinkImage" name="drinkImage" value="'
					+ src + '"/>');
}
